//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================


package com.roguetreasure.destroytheworld;

import java.util.TreeMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

class SoundSfxMgr
{
	private static final String LOG_TAG = "SoundSfxMgr: ";
	private final int 	MAX_SFX_STREAMS = 28;
	
	private DestroyWorldActivity 	m_oActivity;
	private Context 				m_oContext;
	private SoundPool 				m_oSoundPool = null;
	private AudioManager  			m_oAudioManager;
	private TreeMap<String,Integer> m_oNameToId = new TreeMap<String, Integer>();
	private float 					m_f32Volume 	= 0.99f;
	private int						m_iNextSndId 	= 0;
	
	private boolean					m_bInitialized = false;
	
	SoundSfxMgr() 
	{
	}
    public void Startup( DestroyWorldActivity oActivity )
    {
    	m_oActivity = oActivity;
    	m_oContext = m_oActivity;
		m_oAudioManager = (AudioManager)m_oContext.getSystemService(Context.AUDIO_SERVICE);
		//re-init sound pool to work around bugs
		ReleaseSoundPool();

		m_oSoundPool = new SoundPool( MAX_SFX_STREAMS, AudioManager.STREAM_MUSIC, 100 );
		// this sucks.. must preload sounds because
		// otherwise sounds will not be ready first time loaded and will not
		// play first attempt. In level 8 sdk the sound could be played as soon
		// as load completes by using OnLoadCompleteListener
		
		// NOTE: OGG format must be 16 bit, 48000 hz, 192kbs, CBR mode
		SfxLoadSound("choice2");
		SfxLoadSound("fireball"); 
		SfxLoadSound("button24");
		SfxLoadSound("explode1"); 
		SfxLoadSound("sizzel");
//		SfxLoadSound("go");
		
		//Log.e("SoundSfxMgr", "Preload Resources Done");
		m_bInitialized = true;
		
		
    }
    public void Shutdown()
    {
    	ReleaseSoundPool();
    }
	public void ReleaseSoundPool()
	{
		if( m_oSoundPool != null)
		{
			m_oSoundPool.release();
			m_oSoundPool = null;
		}
	}
    //------------------------------------------------------------------------
    //! get new sound id
    int  GetNextSoundId()
    {
    	m_iNextSndId++;
    	return m_iNextSndId;
    }
    //! load sound into cache 
	public int SfxLoadSound(String strSndFile)
	{
		// get path to resource then get resource id
		
		// get resource id from full file path
		String strSndResource = m_oContext.getPackageName()+":raw/" + strSndFile;
		//Log.i(LOG_TAG, "SfxLoadSound: loading " + strSndResource );
		
		int iSndResourceId = m_oContext.getResources().getIdentifier(strSndResource, null, null);
		if( iSndResourceId == 0 )
		{
			// could not locate resource
			//Log.e("SoundSfxMgr", "Could not open Sound Resource" + strSndResource);
			return 0;
		}
		int iSndId = m_oSoundPool.load(m_oContext, iSndResourceId, 1);
		
		// add sound association to our map of file to id
		m_oNameToId.put(strSndFile, iSndId);
		//Log.e("SoundSfxMgr", "Assigned sound " + strSndResource + " to " + iSndId);
		
		return iSndId;
	}
    //------------------------------------------------------------------------
    //! play sound
	public void SfxPlay( String strSndFile )
	{
		if( false == m_bInitialized )
		{
			return;
		}
		//Log.i(LOG_TAG, "SfxPlay: file " + strSndFile );
		if( strSndFile.length() == 0 )
		{
			Log.i(LOG_TAG, "SfxPlay: null sound file name" );
			return;
		}
		// Remove the extension if exists.. 
	    int iExtensionIndex = strSndFile.lastIndexOf("."); 
	    if (iExtensionIndex > 0 )
	    {
	    	strSndFile = strSndFile.substring(0, iExtensionIndex);
	    }

		int iSndId = 0;
		if( m_oNameToId.containsKey(strSndFile) )
		{
			iSndId = m_oNameToId.get(strSndFile);
		}
		
		
		if( iSndId <= 0 )
		{
			// sound was not previously loaded.. load into sound pool cache and get sound id
			iSndId = SfxLoadSound( strSndFile );
			if( iSndId <= 0 )
			{
				// sound could not be loaded
				Log.i(LOG_TAG, "SfxPlay: Could Not load file" + strSndFile );
				return;
				
			}
		}
		float streamVolume = m_oAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		streamVolume = streamVolume / m_oAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		// play non looped
		m_oSoundPool.play( iSndId, m_f32Volume, m_f32Volume, 1, 0, 1.0f );
		
		// to play looping
		//m_oSoundPool.play( iSndId, streamVolume, streamVolume, 1, -1, 1.0f);

	}
	public void SfxPause( String strSndFile )
	{
		if( false == m_bInitialized )
		{
			return;
		}
		//m_oSoundpool.pause(1);
	}
	
	public void SfxStop( String strSndFile )
	{
		if( false == m_bInitialized )
		{
			return;
		}
		//m_oSoundpool.pause(1);
	}
	
	public void SfxSetVolume( float f32Volume )
	{
		if( f32Volume >= 1.0f )
		{
			f32Volume = 0.99f;
		}
		m_f32Volume = f32Volume;
	}
};


